﻿#region License
/**	
The MIT License (MIT)

Copyright (c) 2013 Philipp Schillinger, Michael Alekseew, Andreas Braun, Steeven Zeiß

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. 
 **/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;

namespace IndoorLocalization.UserInterface {
	/// <summary>
	/// Realizes a window that gives a short overview about the current map coverage.
	/// Displays the coverage map as well as the percentages of covering.
	/// </summary>
	public partial class CoverageMapWindow : Form {

		/// <summary>
		/// Delegate that could be used to update the progress bar.
		/// Can be used as an additional parameter by methods that support visualizing their progress.
		/// </summary>
		/// <param name="percent">The current progress in percent.</param>
		[Obsolete("Progress bar is in marquee mode.")]
		public delegate void ProgressCallback(int percent);

		/// <summary>
		/// The coverage map to be displayed.
		/// </summary>
		private Bitmap coverageMap;

		/// <summary>
		/// Displays a new coverage map window with the given coverage map.
		/// </summary>
		/// <param name="coverage">The coverage map that should be used by this window.</param>
		public CoverageMapWindow(Bitmap coverage) {
			InitializeComponent();

			this.coverageMap = coverage;
		}

		/// <summary>
		/// While loading this window, calculate percentages.
		/// It is not neccessary to calculate the coverage map because it is already given.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void CoverageMapWindow_Load(object sender, EventArgs e) {

			// indicate that calculation is being done
			progressBar.Style = ProgressBarStyle.Marquee;

			ThreadPool.QueueUserWorkItem(delegate {
				// calculate percentages and convert coverage map
				int[] percentages = FloorPlan.CalcCoveragePercentages();
				Image<Bgr, Byte> toDisplay = new Image<Bgr, byte>(coverageMap);

				// draw cams onto map
				foreach (SensorCam cam in Program.Cameras) {
					toDisplay.Draw(new Rectangle(cam.X, FloorPlan.MaxY - cam.Y, 3, 3), new Bgr(Color.Blue), 1);
				}

				// set the calculated values of all window elements and stop the progress bar
				covMapBox.Invoke(new MethodInvoker(delegate {
					progressBar.Style = ProgressBarStyle.Blocks;
					covMapBox.Image = toDisplay;
					covState0.Text = percentages[0] + " %";
					covState1.Text = percentages[1] + " %";
					covState2.Text = percentages[2] + " %";
					covState3.Text = percentages[3] + " %";
					covTotalNot.Text = Math.Min(100, percentages[0] + percentages[1]) + " %";
					covTotal.Text = Math.Min(100, percentages[2] + percentages[3]) + " %";
				}));
			});
		}

	}
}
